import axios from 'axios'
import type { AxiosResponse, InternalAxiosRequestConfig } from 'axios'
const baseURL = import.meta.env.VITE_API_BASE_URL

export const http = axios.create({
  baseURL: baseURL,
  timeout: 10000,
})

// 请求拦截器
http.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`
    }
    return config
  },
  (error) => Promise.reject(error),
)

// 响应拦截器
http.interceptors.response.use(
  (response: AxiosResponse) => response.data,
  (error) => {
    if (error.response?.status === 401) {
      localStorage.removeItem('token')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  },
)

export default http
